file(MAKE_DIRECTORY "${CURRENT_DIR}/build")

set(CLANG_TIDY_BUILD_INFO_PATH "${CURRENT_DIR}/build/build_info.txt")


execute_process(
  COMMAND ${CMAKE_CXX_COMPILER} -dumpmachine
  OUTPUT_VARIABLE TARGET_TRIPLE
  OUTPUT_STRIP_TRAILING_WHITESPACE
  ERROR_QUIET)

execute_process(
  COMMAND ${CMAKE_CXX_COMPILER} -print-sysroot
  OUTPUT_VARIABLE SYSROOT
  OUTPUT_STRIP_TRAILING_WHITESPACE
  ERROR_QUIET)

file(WRITE  "${CLANG_TIDY_BUILD_INFO_PATH}" "CXX=${CMAKE_CXX_COMPILER}\n")
file(APPEND "${CLANG_TIDY_BUILD_INFO_PATH}" "CC=${CMAKE_C_COMPILER}\n")
file(APPEND "${CLANG_TIDY_BUILD_INFO_PATH}" "CVI_PLATFORM=${CVI_PLATFORM}\n")
file(APPEND "${CLANG_TIDY_BUILD_INFO_PATH}" "CMAKE_TOOLCHAIN_FILE=${TOOLCHAIN_PATH}\n")
file(APPEND "${CLANG_TIDY_BUILD_INFO_PATH}" "TARGET_TRIPLE=${TARGET_TRIPLE}\n")
file(APPEND "${CLANG_TIDY_BUILD_INFO_PATH}" "SYSROOT=${SYSROOT}\n")


#generate build_info.txt in install directory,contains CVI_PLATFORM,git commit id,build time

set(RELEASE_BUILD_INFO_PATH "${CMAKE_BINARY_DIR}/build_info.txt")
message(STATUS "RELEASE_BUILD_INFO_PATH: ${RELEASE_BUILD_INFO_PATH}")
# get git commit id (short)
if(EXISTS "${CURRENT_DIR}/.git")
  execute_process(
    COMMAND git rev-parse --short HEAD
    WORKING_DIRECTORY ${CURRENT_DIR}
    OUTPUT_VARIABLE GIT_COMMIT_ID
    OUTPUT_STRIP_TRAILING_WHITESPACE
    ERROR_QUIET)
else()
  set(GIT_COMMIT_ID "unknown")
endif()

# get build time
string(TIMESTAMP BUILD_TIME "%Y-%m-%d %H:%M:%S %z")

# append info
file(WRITE "${RELEASE_BUILD_INFO_PATH}" "GIT_COMMIT_ID=${GIT_COMMIT_ID}\n")
file(APPEND "${RELEASE_BUILD_INFO_PATH}" "CVI_PLATFORM=${CVI_PLATFORM}\n")
file(APPEND "${RELEASE_BUILD_INFO_PATH}" "BUILD_TIME=${BUILD_TIME}\n")

# install the file to install root as build_info.txt
install(FILES ${RELEASE_BUILD_INFO_PATH} DESTINATION .)
